package com.hqyj.lyf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.hqyj.lyf.config.MyRedisConfig;
import com.hqyj.lyf.mapper.UserMapper;
import com.hqyj.lyf.pojo.User;
import com.hqyj.lyf.service.UserService;
import com.hqyj.lyf.utils.MD5;
import com.hqyj.lyf.utils.ResultInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    // 注入UserMapper接口
    @Autowired
    private UserMapper userMapper;

    //注入RedisTemplate
    @Autowired
    private MyRedisConfig myRedisConfig;


    // 登录
    @Override
    public ResultInfo login(User user) {
        // 使用shiro登录步骤
        // 判断用户是否为null，为null直接返回
        if (user == null){
            return new ResultInfo(500,"输入用户数据有误",user);
        }
        //用户不为null的情况
        // 获取主体subject
        Subject subject = SecurityUtils.getSubject();
        if (!subject.isAuthenticated()){
            // 未登录
            // 创建当前登录对象的token
            AuthenticationToken token = new UsernamePasswordToken(user.getUsername(),user.getPassword()); // 需要传入参数，登录用户名和密码
            // 把当前token传入到shiro提供的登录方法，执行用户身份认证和授权
            try {
                subject.login(token);
                User getUser = userMapper.selectOne(new QueryWrapper<User>().eq("username", user.getUsername()));
                return new ResultInfo(200,"登录成功",getUser.getName());
            }catch (UnknownAccountException unknownAccountException){ //用户名不存在异常
                return new ResultInfo(-1,"用户名不存在",user);
            }catch (IncorrectCredentialsException incorrectCredentialsException){ //密码不正确异常
                return new ResultInfo(-2,"密码不正确",user);
            }catch (Exception exception){
                return new ResultInfo(-3,"其他异常或者密码格式不对",user);
            }
        }else{
            // 登录了
            return new ResultInfo(200,"已经登陆了");
        }
    }

    // 删除单条考生数据
    @Override
    public ResultInfo examineeDeleteByUserId(User user) {
        int result = userMapper.deleteById(user.getUserId());
        if (result > 0){
            return new ResultInfo(200,"删除成功",result);
        }else {
            return new ResultInfo(-1,"删除失败",result);
        }

    }

    // 修改单条考生数据
    @Override
    public ResultInfo examineeUpdate(User user) {
        int result = userMapper.updateById(user);
        if (result > 0){
            return new ResultInfo(200,"修改成功",result);
        }else {
            return new ResultInfo(-1,"修改失败",null);
        }
    }

    // 添加考生信息
    @Override
    public ResultInfo examineeAdd(User user) {
        String newPassword = MD5.Md5(user.getUsername(), user.getPassword());
        System.out.println("--------------"+newPassword);
        user.setPassword(newPassword);
        int result = userMapper.insert(user);
        if (result > 0){
            return new ResultInfo(200,"添加成功",result);
        }else {
            return new ResultInfo(-1,"添加失败",result);
        }
    }

    // 批量删除
    @Override
    public ResultInfo examineeBatchDel(int[] arr) {
        // 注意这里传入参数应该为集合，所以转一下
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = 0; i < arr.length; i++) {
            list.add(arr[i]);
        }
        int result = userMapper.delete(new UpdateWrapper<User>().in("user_id", list));
        if (result > 0){
            return new ResultInfo(200,"批量删除成功",result);
        }else {
            return new ResultInfo(-1,"批量删除失败",result);
        }
    }
}
